<?php

namespace App\Http;

use App\Constant\Constant;
use App\Models\UserToken;
use GuzzleHttp\Client;

class Request
{
    const HTTP_X_SIGN        = 'HTTP_X_SIGN';
    const HTTP_X_TIMESTAMP   = 'HTTP_X_TIMESTAMP';
    const HTTP_X_ERROR_TYPE  = 'HTTP_X_ERROR_TYPE';
    const HTTP_X_USER_TOKEN  = 'HTTP_X_USER_TOKEN';
    const HTTP_X_ADMIN_TOKEN = 'HTTP_X_ADMIN_TOKEN';
    const HTTP_REFERER       = 'HTTP_REFERER';
    const HTTP_X_APP_ID      = 'HTTP_X_APP_ID';
    const HTTP_X_CLIENT      = 'HTTP_X_CLIENT';
    const HTTP_X_CLIENT_TYPE      = 'HTTP_X_CLIENT_TYPE';//登陆类型

    const CLIENT_MINI = 'user_minapp';
    const CLIENT_APP  = 'user_app';
    const CLIENT_PC   = 'user_pc';


    const NOT_STANDARD_HTTP_RESPONSE_CODE = 0;
    const STANDARD_HTTP_RESPONSE_CODE     = 1;

    const TIMEOUT = 30;

    const GET_METHOD     = 'GET';
    const POST_METHOD    = 'POST';
    const OPTIONS_METHOD = 'OPTIONS';
    const IS_JSON        = true;

    protected static $errorType = false;

    public static function getSign()
    {
        return array_get($_SERVER, self::HTTP_X_SIGN);
    }

    public static function getTimestamp()
    {
        return array_get($_SERVER, self::HTTP_X_TIMESTAMP);
    }

    public static function getUserToken()
    {
        return request()->header('x-user-token');
//        return array_get($_SERVER, self::HTTP_X_USER_TOKEN);
    }

    public static function getAdminToken()
    {
        return array_get($_SERVER, self::HTTP_X_ADMIN_TOKEN);
    }

    public static function getReferer()
    {
        return array_get($_SERVER, self::HTTP_REFERER);
    }

    public static function getAppId()
    {
        return array_get($_SERVER, self::HTTP_X_APP_ID);
    }

    public static function getClient()
    {
        return array_get($_SERVER, self::HTTP_X_CLIENT);
    }
    public static function getClientTypeX()
    {
        return array_get($_SERVER, self::HTTP_X_CLIENT_TYPE);
    }

    public static function clientType()
    {
        $client = self::getClient();
        if ($client == self::CLIENT_APP) {
            return Constant::ClientType_App;
        } else if ($client == self::CLIENT_MINI) {
            return Constant::ClientType_MinApp;
        } else if ($client == self::CLIENT_PC) {
            return Constant::ClientType_PC;
        }
        return Constant::ClientType_PC;
    }

    public static function getClientType()
    {
        $client = self::getClient();
        if ($client == self::CLIENT_APP) {
            return Constant::ClientType_App;
        } else if ($client == self::CLIENT_MINI) {
            return Constant::ClientType_MinApp;
        } else if ($client == self::CLIENT_PC) {
            return Constant::ClientType_PC;
        }
        return Constant::ClientType_PC;
    }

    public static function setUserToken($token)
    {
        $_SERVER[self::HTTP_X_USER_TOKEN] = $token;
    }

    public static function setClient($client)
    {
        $_SERVER[self::HTTP_X_CLIENT] = $client;
    }

    protected static function getErrorType()
    {
        if (self::$errorType !== false) {
            return self::$errorType;
        }

        $errorType       = intval(array_get($_SERVER, self::HTTP_X_ERROR_TYPE, 1));
        self::$errorType = in_array($errorType, [
            self::NOT_STANDARD_HTTP_RESPONSE_CODE,
            self::STANDARD_HTTP_RESPONSE_CODE
        ], true) ? $errorType : self::STANDARD_HTTP_RESPONSE_CODE;

        return self::$errorType;
    }

    public static function isStandardResponse()
    {
        return self::getErrorType() == self::STANDARD_HTTP_RESPONSE_CODE;
    }

    public static function client($host, $port = 80)
    {
        return new Client([
            'base_uri'        => self::buildUrl($host, $port),
            'timeout'         => self::TIMEOUT,
            'allow_redirects' => false,
        ]);
    }

    /**
     * @param $host
     * @param $port
     * @return string
     */
    public static function buildUrl($host, $port)
    {
        if ($port == 80 || $port == 443) {
            return $host;
        } else {
            return "$host:$port";
        }
    }

    /**
     * @param array $params
     * @param $method
     * @param $isJson
     * @return array
     */
    public static function buildQuery($params, $method, $isJson = !self::IS_JSON)
    {
        if (!$params) {
            return [];
        }

        if ($isJson) {
            return [
                'json' => $params
            ];
        }

        if (strtoupper($method) == self::GET_METHOD) {
            return [
                'query' => $params,
            ];
        } elseif (strtoupper($method) == self::POST_METHOD) {
            return [
                'form_params' => $params,
            ];
        } else {
            return [];
        }
    }

    public static function buildHost($scheme, $host)
    {
        return $scheme . '://' . $host;
    }
}
